package net.tycmc.cty.ctyms.timejob.iems.dao;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import net.tycmc.bulb.common.dao.IBaseDAO;
import net.tycmc.bulb.common.dao.ITransactionDAO;
import net.tycmc.bulb.common.util.MapGetter;
import net.tycmc.cty.ctyms.timejob.common.ConfigEnum;

/**
 * 故障报警相关表的DAO操作
 * 
 * @modify chengShiChao 增加故障报警状态初始化 2011-10-14
 * @modify chengShiChao 增加故障报警、保养提醒信息的短信发送和保养预测轮询 2011-10-22
 * @remark 故障报警、保养提醒的程序轮询时间间隔5分钟，保养预测程序轮询时间间隔1小时
 */
public class StopCallModeDao {
	IBaseDAO baseDAO;
	private ITransactionDAO tranDAO;

	public IBaseDAO getBaseDAO() {
		return baseDAO;
	}

	public void setBaseDAO(IBaseDAO baseDAO) {
		this.baseDAO = baseDAO;
	}

	public ITransactionDAO getTranDAO() {
		return tranDAO;
	}

	public void setTranDAO(ITransactionDAO tranDAO) {
		this.tranDAO = tranDAO;
	}

	/**
	 * 获取需要进行终止采集的数据集合
	 * 终止采集时间大于当前时间+5分钟（当前时间取自数据库时间）
	 * @return
	 */
	public List<Map<String,Object>> getCRRecord(){
		String sql = "select CR_ID,CR_Vcl_ID,CR_State,CR_BgnTime,CR_EndTime,CR_AccountID,CR_MsgPSM_ID,CR_Start_MsgTlS_ID,CR_Stop_MsgTlS_ID,CR_isValid,CR_Remark,CR_ParamType "
			+"from CollectionRecord " 
			+"where CR_isValid=1 and CR_State=1 and CR_EndTime<DATEADD(MINUTE,5,GETDATE()) and CR_Stop_MsgTlS_ID is null ";
		return baseDAO.execQuery(sql, new Object[]{});
	}
	
	/**
	 * 发送终止指令并更新采集记录
	 * @param map
	 * @return
	 */
	public boolean updateCR(Map<String,Object> map){
		//设备ID
		String CR_Vcl_ID = MapGetter.getString(map, "CR_Vcl_ID");
		//采集记录
		String CR_ID = MapGetter.getString(map, "CR_ID");
		//采集参数类型	1:公有	2：专有
		String CR_ParamType = MapGetter.getString(map, "CR_ParamType");
		String MsgTlS_SendTarget = "";
		
		List<String> sqlList = new ArrayList<String>();
		List<Object[]> paramList = new ArrayList<Object[]>();
		
		if(CR_ParamType.equals("1")){
			MsgTlS_SendTarget = ConfigEnum.SendTarget_TS1.getName();
			sqlList.add("insert into CTY_Egn_B.dbo.Msg_PGN_Set_Mode(MsgPSM_Vcl_ID,MsgPSM_SetTime,MsgPSM_TimeOver,MsgPSM_SysCT_ID,MsgPSM_SysCS_ID,MsgPSM_PGNCount,MsgPSM_SendTarget,MsgPSM_Mode) "
				+"values (?,GETDATE(),0,?,?,?,?,?);");
			paramList.add(new Object[]{CR_Vcl_ID,1,1,0,MsgTlS_SendTarget,ConfigEnum.SendTarget_KZ.getName()});
			
			//修改采集记录中的启动特殊模式指令ID
			sqlList.add( "update CollectionRecord set CR_Stop_MsgTlS_ID=IDENT_CURRENT('CTY_Egn_B.dbo.Msg_PGN_Set_Mode'),CR_isValid=1,CR_Remark='自动终止采集呼叫中...' where CR_ID=?");
			paramList.add(new Object[]{CR_ID});
			
		}else if(CR_ParamType.equals("2")){
			MsgTlS_SendTarget = ConfigEnum.SendTarget_TS2.getName();
			//向特殊模式设置采集PGN参数信息表插入数据
			sqlList.add("insert into CTY_Egn_B.dbo.Msg_UDS_Set_Mode(MsgUSM_Vcl_ID,MsgUSM_SetTime,MsgUSM_TimeOver,MsgUSM_SysCT_ID,MsgUSM_SysCS_ID,MsgUSM_UDSCount,MsgUSM_SendTarget,MsgUSM_Mode) "
				+"values (?,GETDATE(),0,?,?,?,?,?);");
			paramList.add(new Object[]{CR_Vcl_ID,1,1,0,MsgTlS_SendTarget,ConfigEnum.SendTarget_KZ.getName()});
			
			//修改采集记录中的启动特殊模式指令ID
			sqlList.add( "update CollectionRecord set CR_Stop_MsgTlS_ID=IDENT_CURRENT('CTY_Egn_B.dbo.Msg_UDS_Set_Mode'),CR_isValid=1,CR_Remark='自动终止采集呼叫中...' where CR_ID=?");
			paramList.add(new Object[]{CR_ID});
		}
		return tranDAO.batch(sqlList, paramList);
	}
}
